package com.ruoyi.system.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OSSClientConfig {

    @Autowired
    private OSSConfig ossConfig;

    @Bean
    public OSSClient ossClient() {
        // 创建凭证提供器
        DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
                ossConfig.getAccessKeyId(), 
                ossConfig.getAccessKeySecret()
        );

        // 创建客户端配置
        ClientConfiguration config = new ClientConfiguration();
        config.setMaxConnections(ossConfig.getMaxConnections());
        config.setSocketTimeout(ossConfig.getSocketTimeout());
        config.setConnectionTimeout(ossConfig.getConnectionTimeout());
        config.setConnectionRequestTimeout(ossConfig.getConnectionRequestTimeout());
        config.setIdleConnectionTime(ossConfig.getIdleTime());
        
        // 支持CNAME
        config.setSupportCname(true);
        
        // 创建OSSClient实例
        return new OSSClient(ossConfig.getEndpoint(), credentialProvider, config);
    }
}